VERSION 5.00
Begin VB.Form MMIWriteVal 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "Form1"
   ClientHeight    =   1440
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   5775
   Icon            =   "f4 MMIWriteVal.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   1440
   ScaleWidth      =   5775
   ShowInTaskbar   =   0   'False
   StartUpPosition =   3  'Windows Default
   Begin VB.OptionButton SetDig 
      Caption         =   "On Txt"
      Height          =   255
      Index           =   1
      Left            =   4080
      TabIndex        =   6
      Top             =   480
      Width           =   1695
   End
   Begin VB.OptionButton SetDig 
      Caption         =   "Off Txt"
      Height          =   255
      Index           =   0
      Left            =   4080
      TabIndex        =   5
      Top             =   120
      Width           =   1575
   End
   Begin VB.TextBox NewVal 
      Height          =   285
      Left            =   4080
      TabIndex        =   2
      Text            =   "Text1"
      Top             =   120
      Width           =   1335
   End
   Begin VB.CommandButton pB 
      Caption         =   "Accept"
      Default         =   -1  'True
      Height          =   375
      Index           =   1
      Left            =   4080
      TabIndex        =   1
      Top             =   960
      Width           =   735
   End
   Begin VB.CommandButton pB 
      Cancel          =   -1  'True
      Caption         =   "Cancel"
      Height          =   375
      Index           =   0
      Left            =   4920
      TabIndex        =   0
      Top             =   960
      Width           =   735
   End
   Begin VB.Label Link 
      BorderStyle     =   1  'Fixed Single
      Caption         =   "Link"
      Height          =   255
      Left            =   3240
      TabIndex        =   4
      Top             =   0
      Visible         =   0   'False
      Width           =   495
   End
   Begin VB.Label Prompt 
      BorderStyle     =   1  'Fixed Single
      Caption         =   "Prompt"
      Height          =   1215
      Left            =   120
      TabIndex        =   3
      Top             =   120
      Width           =   3735
      WordWrap        =   -1  'True
   End
End
Attribute VB_Name = "MMIWriteVal"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub Form_Load()
    
    Me.Caption = LAY.Title & " - Set New Value"
    Me.Left = LAY.SETLeft
    Me.Top = LAY.SETTop
    pB(1).Enabled = False 'Accept PB
    
    'Gather the cur values of those to be adjusted.
    Dim pT: pT = Vdt.Index
    Prompt = "Attention:" & vbNewLine
    Prompt = Prompt & "Desc: " & Vdt.Desc(pT).Caption & vbNewLine
    Prompt = Prompt & "Address: " & Vdt.Link(pT).LinkTopic & "!" & Vdt.Item(pT).LinkWriteItem & vbNewLine
    Prompt = Prompt & "Current Value: " & Vdt.Disp(pT).Caption & vbNewLine
    
    Select Case Vdt.Item(pT).Type
        Case 0 'RAW
            SetDig(0).Visible = False
            SetDig(1).Visible = False
            NewVal = Val(Vdt.Disp(pT).Caption)
            Me.Show
            NewVal.SelStart = 1
            NewVal.SelStart = 9
            NewVal.SetFocus
        Case 1 'Digital On/Off
            SetDig(0).Caption = Vdt.Item(pT).OffState
            SetDig(1).Caption = Vdt.Item(pT).OnState
            If SetDig(0).Caption = Vdt.Disp(pT).Caption Then
                SetDig(0).Value = True
                SetDig(1).Value = False
            End If
            If SetDig(1).Caption = Vdt.Disp(pT).Caption Then
                SetDig(0).Value = False
                SetDig(1).Value = True
            End If
            NewVal.Visible = False
            Me.Show
            
        Case 2 'Scaled Analog
            SetDig(0).Visible = False
            SetDig(1).Visible = False
            NewVal = Val(Vdt.Disp(pT).Caption)
            Me.Show
            NewVal.SelStart = 1
            NewVal.SelStart = 9
            NewVal.SetFocus
    End Select
            
            pB(1).Enabled = False 'Accept PB

End Sub

Private Sub Form_Unload(Cancel As Integer)
    LAY.SETLeft = Me.Left
    LAY.SETTop = Me.Top
End Sub

Private Sub SetDig_Click(Index As Integer)
    pB(1).Enabled = True
End Sub

Private Sub NewVal_Change()
    pB(1).Enabled = True
End Sub

Private Sub NewVal_GotFocus()
    NewVal.SelStart = 0
    NewVal.SelLength = Len(NewVal)
End Sub

Private Sub pB_Click(Index As Integer)
    Select Case Index
        Case 0
            Unload Me
        Case 1
            WriteNewVal
            Unload Me
    End Select
End Sub

Private Sub WriteNewVal()

Dim pT As Integer: pT = Vdt.Index
Select Case Vdt.Item(pT).Type
    Case 0
        Link.Caption = Trim(NewVal)
    Case 1
        If SetDig(0).Value = True Then Link.Caption = 0
        If SetDig(1).Value = True Then Link.Caption = 1
    Case 2
        'RAW = ScaledValue - Offset / Scale
        Link.Caption = Round((Val(Trim(NewVal)) - Vdt.Item(pT).Offset) / Vdt.Item(pT).Scl, Vdt.Item(pT).Round)
End Select
'DDE link often produce a timeout error
On Error Resume Next
Link.LinkTimeout = 5
Link.LinkTopic = Vdt.Link(Vdt.Index).LinkTopic
Link.LinkItem = Vdt.Item(Vdt.Index).LinkWriteItem
Link.LinkMode = vbLinkManual
Link.LinkPoke
Link.LinkMode = vbLinkNone
On Error GoTo 0
End Sub
